﻿using JSM.DataStore;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace JSM.Test
{
	[TestClass]
	public class JavaScriptModelTest
	{
		[TestMethod]
		public void TestJavaScriptInsertFunctionWithDefaultDataStore()
		{
			UnitTestHelper.TestFunctionWithDefaultDataStore(TestJavaScriptInsertFunction);
		}

		[TestMethod]
		public void TestJavaScriptInsertFunctionWithoutDefaultDataStore()
		{
			UnitTestHelper.TestFunctionWithoutDefaultDataStore(TestJavaScriptInsertFunction);
		}

		[TestMethod]
		public void TestJavaScriptAddFunctionWithDefaultDataStore()
		{
			UnitTestHelper.TestFunctionWithDefaultDataStore(TestJavaScriptAddFunction);
		}

		[TestMethod]
		public void TestJavaScriptAddFunctionWithoutDefaultDataStore()
		{
			UnitTestHelper.TestFunctionWithoutDefaultDataStore(TestJavaScriptAddFunction);
		}

		[TestMethod]
		public void TestJavaScriptAddVariablesWithDefaultDataStore()
		{
			UnitTestHelper.TestFunctionWithDefaultDataStore(TestJavaScriptAddVariables);
		}

		[TestMethod]
		public void TestJavaScriptAddVariablesWithoutDefaultDataStore()
		{
			UnitTestHelper.TestFunctionWithoutDefaultDataStore(TestJavaScriptAddVariables);
		}

		[TestMethod]
		public void TestJavaScriptAddVariableWithDefaultDataStore()
		{
			UnitTestHelper.TestFunctionWithDefaultDataStore(TestJavaScriptAddVariable);
		}

		[TestMethod]
		public void TestJavaScriptAddVariableWithoutDefaultDataStore()
		{
			UnitTestHelper.TestFunctionWithoutDefaultDataStore(TestJavaScriptAddVariable);
		}
		
		private void TestJavaScriptInsertFunction(IJavaScriptModelAware javaScriptModelAware, IDataStoreProvider dataStoreProvider)
		{
			javaScriptModelAware.AddJavaScriptFunction("GlobalFunction", PageLoadEvent.Ready, dataStoreProvider);
			Assert.IsTrue(JavaScriptModel.GetJavaScriptModel(javaScriptModelAware, dataStoreProvider).HasFunctions);
			Assert.AreEqual(1, JavaScriptModel.GetJavaScriptModel(javaScriptModelAware, dataStoreProvider).GetFunctions().Count);
			Assert.AreEqual("GlobalFunction", JavaScriptModel.GetJavaScriptModel(javaScriptModelAware, dataStoreProvider).GetFunctions()[0].Function);
			Assert.AreEqual(null, JavaScriptModel.GetJavaScriptModel(javaScriptModelAware, dataStoreProvider).GetFunctions()[0].Parameters);

			javaScriptModelAware.InsertJavaScriptFunction("GlobalFunction2", PageLoadEvent.Ready, dataStoreProvider);
			Assert.IsTrue(JavaScriptModel.GetJavaScriptModel(javaScriptModelAware, dataStoreProvider).HasFunctions);
			Assert.AreEqual(2, JavaScriptModel.GetJavaScriptModel(javaScriptModelAware, dataStoreProvider).GetFunctions().Count);
			Assert.AreEqual("GlobalFunction2", JavaScriptModel.GetJavaScriptModel(javaScriptModelAware, dataStoreProvider).GetFunctions()[0].Function);
			Assert.AreEqual(null, JavaScriptModel.GetJavaScriptModel(javaScriptModelAware, dataStoreProvider).GetFunctions()[0].Parameters);
		}

		private void TestJavaScriptAddFunction(IJavaScriptModelAware javaScriptModelAware, IDataStoreProvider dataStoreProvider)
		{
			javaScriptModelAware.AddJavaScriptFunction("GlobalFunction", PageLoadEvent.Ready, dataStoreProvider);
			Assert.IsTrue(JavaScriptModel.GetJavaScriptModel(javaScriptModelAware, dataStoreProvider).HasFunctions);
			Assert.AreEqual(1, JavaScriptModel.GetJavaScriptModel(javaScriptModelAware, dataStoreProvider).GetFunctions().Count);
			Assert.AreEqual("GlobalFunction", JavaScriptModel.GetJavaScriptModel(javaScriptModelAware, dataStoreProvider).GetFunctions()[0].Function);
			Assert.AreEqual(null, JavaScriptModel.GetJavaScriptModel(javaScriptModelAware, dataStoreProvider).GetFunctions()[0].Parameters);

			javaScriptModelAware.AddJavaScriptFunction("SomeLiteral.Function", PageLoadEvent.Init, dataStoreProvider);
			Assert.IsTrue(JavaScriptModel.GetJavaScriptModel(javaScriptModelAware, dataStoreProvider).HasFunctions);
			Assert.AreEqual(1, JavaScriptModel.GetJavaScriptModel(javaScriptModelAware, dataStoreProvider).GetFunctions(PageLoadEvent.Init).Count);
			Assert.AreEqual("SomeLiteral.Function", JavaScriptModel.GetJavaScriptModel(javaScriptModelAware, dataStoreProvider).GetFunctions(PageLoadEvent.Init)[0].Function);
			Assert.AreEqual(null, JavaScriptModel.GetJavaScriptModel(javaScriptModelAware, dataStoreProvider).GetFunctions(PageLoadEvent.Init)[0].Parameters);

			javaScriptModelAware.AddJavaScriptFunction("SomeLiteral.AnotherLiteral.Function", PageLoadEvent.Ready, dataStoreProvider, "FirstParam", true, 1234);
			Assert.IsTrue(JavaScriptModel.GetJavaScriptModel(javaScriptModelAware, dataStoreProvider).HasFunctions);
			Assert.AreEqual(2, JavaScriptModel.GetJavaScriptModel(javaScriptModelAware, dataStoreProvider).GetFunctions().Count);
			Assert.AreEqual("SomeLiteral.AnotherLiteral.Function", JavaScriptModel.GetJavaScriptModel(javaScriptModelAware, dataStoreProvider).GetFunctions()[1].Function);
			Assert.AreEqual(3, JavaScriptModel.GetJavaScriptModel(javaScriptModelAware, dataStoreProvider).GetFunctions()[1].Parameters.Count);
		}

		private void TestJavaScriptAddVariables(IJavaScriptModelAware javaScriptModelAware, IDataStoreProvider dataStoreProvider)
		{
			javaScriptModelAware.AddJavaScriptVariables(PageLoadEvent.Ready, dataStoreProvider, "Test1", "Some value", "Test2", 123);
			Assert.IsTrue(JavaScriptModel.GetJavaScriptModel(javaScriptModelAware, dataStoreProvider).HasVariables);
			Assert.AreEqual(2, JavaScriptModel.GetJavaScriptModel(javaScriptModelAware, dataStoreProvider).GetVariables().Count);
			Assert.AreEqual("Some value", JavaScriptModel.GetJavaScriptModel(javaScriptModelAware, dataStoreProvider).GetVariables()["Test1"]);
			Assert.AreEqual(123, JavaScriptModel.GetJavaScriptModel(javaScriptModelAware, dataStoreProvider).GetVariables()["Test2"]);

			javaScriptModelAware.AddJavaScriptVariables(PageLoadEvent.Init, dataStoreProvider, "Test3", true, "Test4", null);
			Assert.IsTrue(JavaScriptModel.GetJavaScriptModel(javaScriptModelAware, dataStoreProvider).HasVariables);
			Assert.AreEqual(2, JavaScriptModel.GetJavaScriptModel(javaScriptModelAware, dataStoreProvider).GetVariables(PageLoadEvent.Init).Count);
			Assert.AreEqual(true, JavaScriptModel.GetJavaScriptModel(javaScriptModelAware, dataStoreProvider).GetVariables(PageLoadEvent.Init)["Test3"]);
			Assert.AreEqual(null, JavaScriptModel.GetJavaScriptModel(javaScriptModelAware, dataStoreProvider).GetVariables(PageLoadEvent.Init)["Test4"]);
		}

		private void TestJavaScriptAddVariable(IJavaScriptModelAware javaScriptModelAware, IDataStoreProvider dataStoreProvider)
		{
			javaScriptModelAware.AddJavaScriptVariable("Test", "Some value", dataStoreProvider: dataStoreProvider);
			Assert.IsTrue(JavaScriptModel.GetJavaScriptModel(javaScriptModelAware, dataStoreProvider).HasVariables);
			Assert.AreEqual(1, JavaScriptModel.GetJavaScriptModel(javaScriptModelAware, dataStoreProvider).GetVariables().Count);
			Assert.AreEqual("Some value", JavaScriptModel.GetJavaScriptModel(javaScriptModelAware, dataStoreProvider).GetVariables()["Test"]);

			javaScriptModelAware.AddJavaScriptVariable("Test2", 42, PageLoadEvent.Init, dataStoreProvider);
			Assert.IsTrue(JavaScriptModel.GetJavaScriptModel(javaScriptModelAware, dataStoreProvider).HasVariables);
			Assert.AreEqual(1, JavaScriptModel.GetJavaScriptModel(javaScriptModelAware, dataStoreProvider).GetVariables(PageLoadEvent.Init).Count);
			Assert.AreEqual(42, JavaScriptModel.GetJavaScriptModel(javaScriptModelAware, dataStoreProvider).GetVariables(PageLoadEvent.Init)["Test2"]);
		}
	}
}
